package com.pride1952.myoptimusx3;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MeFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MeFragment extends Fragment{
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public static final int EDIT_CARD_ACTIVITY=0;
    View mView;

    private ListView mListView;

    private ImageView imgPhoto;
    private TextView txtName;
    private TextView txtPhone;
    private TextView txtMail;
    private  TextView txtHobby;
    private ImageButton mCard;
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MeFragment.
     */
    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public MeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initWidgets();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView= inflater.inflate(R.layout.fragment_me, container, false);
        initWidgets();
        return mView;
    }

    private void initWidgets(){
        imgPhoto=(ImageView)mView.findViewById(R.id.contact_photo);
        imgPhoto.setBackgroundResource(R.mipmap.ic_launcher);
        txtName=(TextView)mView.findViewById(R.id.contact_name);
        txtPhone=(TextView)mView.findViewById(R.id.contact_number);
        txtMail=(TextView)mView.findViewById(R.id.contact_mail);
        txtHobby=(TextView)mView.findViewById(R.id.contact_hobby);
        SharedPreferences mPreferences=getActivity().getSharedPreferences("MyCard",getActivity().MODE_PRIVATE);
        String name=mPreferences.getString("Name", "");
        String stuNum=mPreferences.getString("StuNum","");
        String number=mPreferences.getString("Number","");
        String mail=mPreferences.getString("Mail","");
        String hobby=mPreferences.getString("Hobby", "");
        txtName.setText(name);
        txtPhone.setText(number);
        txtMail.setText(mail);
        txtHobby.setText(hobby);
        mCard=(ImageButton)mView.findViewById(R.id.imgbtn_me_card_click);
        mCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(getActivity(),EditCardActivity.class);
                startActivityForResult(intent, EDIT_CARD_ACTIVITY);
            }
        });

        mListView=(ListView)mView.findViewById(R.id.me_listView);
        ArrayAdapter mAdapter=new ArrayAdapter<String>(getActivity(),R.layout.me_list_item,getData());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                    {
                        SharedPreferences mPreferences = getActivity().getSharedPreferences("isLogin", getActivity().MODE_PRIVATE);
                        SharedPreferences.Editor mEditor = mPreferences.edit();
                        mEditor.putBoolean("isLogin", false);
                        mEditor.commit();

                        SharedPreferences devicePreference = getActivity().getSharedPreferences("Devices", getActivity().MODE_PRIVATE);
                        SharedPreferences.Editor deviceEditor = devicePreference.edit();
                        deviceEditor.clear();
                        deviceEditor.commit();

                        startActivity(new Intent(getActivity(), WelcomeActivity.class));
                        getActivity().finish();
                    }
                    break;
                    default:
                        break;
                }
            }
        });
    }

    private List<String> getData()
    {
        List<String> data=new ArrayList<String>();
        data.add("注销");
        data.add("反馈");
        data.add("关于OptimusX");
        data.add("联系我们");

        return data;
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        try {
//            mListener = (OnFragmentInteractionListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
//    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode)
        {
            case EDIT_CARD_ACTIVITY:

                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
